package com.zhihaoliang.baseframe.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.zhihaoliang.baseframe.BasicApp;
import com.zhihaoliang.baseframe.db.DataRepository;
import com.zhihaoliang.baseframe.db.entity.CommentEntity;
import com.zhihaoliang.baseframe.db.entity.ProductEntity;

import java.util.List;

/**
 * 创建日期：2020-02-29
 * 描述:
 * 作者:支豪亮
 */
public class ProductViewModel extends AndroidViewModel {

    private final LiveData<ProductEntity> mObservableProduct;

    private final int mProductId;

    private final LiveData<List<CommentEntity>> mObservableComments;


    public ProductViewModel(@NonNull Application application, DataRepository repository,final int productId) {
        super(application);

        mProductId = productId;
        mObservableProduct = repository.loadProduct(productId);
        mObservableComments = repository.loadComment(productId);
    }

    public LiveData<ProductEntity> getProduct() {
        return mObservableProduct;
    }

    public LiveData<List<CommentEntity>> getComments() {
        return mObservableComments;
    }

    public static class Factory extends ViewModelProvider.NewInstanceFactory{

        @NonNull
        private final Application mApplication;

        private final int mProductId;

        private final DataRepository mRepository;

        public Factory(@NonNull Application application,int productId){
            mApplication = application;
            mProductId = productId;
            mRepository = ((BasicApp)application).getRepository();
        }

        @NonNull
        @Override
        public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
            return (T) new ProductViewModel(mApplication,mRepository,mProductId);
        }
    }
}
